TMacros.GossipNPC = nil

function TMacros:QuestLogHasQuest(title)
    local entries, _ = GetNumQuestLogEntries()
    for index = 1, entries do
        local t, level, _, header = GetQuestLogTitle(index)
        if not header and t == title then
            return 1
        end
    end

    return nil
end

-- /run print(TMacros:NPCGossipHasAvailableQuest("长夜未尽", "军需官米兰达"))
function TMacros:NPCGossipHasAvailableQuest(quest, npc)
    if npc and not self:IsCurrentGossipNPC(npc) then 
        return nil 
    end
    return self:NPCGossipHasQuest(quest, GetGossipAvailableQuests())
end

-- /run print(TMacros:NPCGossipHasActiveQuest("长夜未尽", "军需官米兰达"))
function TMacros:NPCGossipHasActiveQuest(quest, npc)
    if npc and not self:IsCurrentGossipNPC(npc) then 
        return nil 
    end
    return self:NPCGossipHasQuest(quest, GetGossipActiveQuests())
end

function TMacros:IsCurrentGossipNPC(npc)
    if not TMacros.GossipNPC then return nil end

    local name = string.match(TMacros.GossipNPC, npc)
    if name == nil then 
        return nil 
    else 
        return 1
    end
end

function TMacros:NPCGossipHasQuest(quest, ...)
    local result = nil
    local index = 0
    for i, value in ipairs(arg) do 
        local newQuest = math.mod(i, 2) == 1
        index = (i + 1) / 2
        if value == quest and newQuest then 
            result = index
        end
    end

    return result
end

function TMacros:GOSSIP_SHOW()
    print("GOSSIP_SHOW")
    TMacros.GossipNPC = GetUnitName("target")
    print(TMacros.GossipNPC)
end

function TMacros:GOSSIP_CLOSED()
    print("GOSSIP_CLOSED")
    TMacros.GossipNPC = nil
end

function TMacros:QUEST_ACCEPT_CONFIRM()
    print("QUEST_ACCEPT_CONFIRM")
end

function TMacros:QUEST_COMPLETE()
    print("QUEST_COMPLETE")
end

-- Called when accepting quest dialog
function TMacros:QUEST_DETAIL()
    print("QUEST_DETAIL")
end

function TMacros:QUEST_FINISHED()
    print("QUEST_FINISHED")
end

function TMacros:QUEST_GREETING()
    print("QUEST_GREETING")
end